package cn.javaweb.group0.model;

import cn.javaweb.entity.User;
import cn.javaweb.group0.entity.Log;
import cn.javaweb.group0.entity.Module;
import cn.javaweb.model.BaseModel;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ModuleModel extends BaseModel {
    public Object parseObject(ResultSet rs) {

        Module module = new Module();

        try {
            module.setId(rs.getInt("id"));
            module.setUrl(rs.getString("url"));
            module.setName(rs.getString("name"));
            module.setRole(rs.getString("role"));
            return module;
        } catch (Exception e) {

        }
        return null;

    }

    public List<Module> getModuleList(int pageNum, int pageLimit) {

        Integer offset = (pageNum-1)*pageLimit;
        String sql = "select * from module limit ?, ?";

        Object[] params = {offset, pageLimit};
        List<Object> list = this.selectAll(sql, params);

        List<Module> re = new ArrayList<Module>();

        for (Object one : list) {
            re.add((Module) one);
        }
        return re;
    }

    public int getTotal() {
        return getCount("select count(*) as cnt from module where 1", null);
    }

    public Module getModule(Integer id) {
        String sql = "select * from module where id=? ";
        Object[] params = {id};

        return (Module) selectOne("select * from module where id=? ", params);
    }

    public Integer deleteModule(Integer id) {
        String sql = "delete from module where id=? ";
        Object[] params = {id};

        return this.update(sql, params);
    }

    public Integer AddModule(Module module) {
        String sql = "insert into module (name, role, url) values (?,?,?)";
        Object[] params = {module.getName(), module.getRole(), module.getUrl()};

        return this.update(sql, params);
    }

    public Integer EditModule(Module module) {
        String sql = "update module set name=? , role=?, url=? where id=? ";
        Object[] params = {module.getName(), module.getRole(), module.getUrl(), module.getId()};

        return this.update(sql, params);
    }
}
